function enableDragReservation(el)
{
	var newEl = $(el).clone();
	
	$('body').append(newEl);
	newEl.removeAttr("onclick");
	newEl.css("border","1px solid #000");
	newEl.attr("dragable","true");
	newEl.css({ opacity: 0.8 });
	newEl.css("-webkit-filter", "brightness(1.2)");
	newEl.css("font-size", "12px");
	newEl.css("position", "absolute");
	newEl.css("height" , $(el).height());
	newEl.css("width" , $(el).width());
	newEl.css("top", $(el).offset().top);
	newEl.css("left", $(el).offset().left);

	$(el).css("display","none");
	
	var minX = $("[dragable]").offset().left;
	var minY = $("#date").offset().top + $("#date").height();
	var maxY = $(".head").not("[roomtype]").last().offset().top;

	$('#EmptyDialog').data('el', newEl);
	$('#EmptyDialog').data('parent', $(el).parent());

	newEl.draggable({axis: "y",
		snap: ".cell",
		snapMode: "inner",
		containment: [minX,minY,minX,maxY],
		drag: function() {
			var el = $('#EmptyDialog').data('el');
			var res = $(el).attr("res");
			var cells = $('#EmptyDialog').data('parent');
			if(cells.attr("colspan") != null && cells.attr("colspan") > 1)
			{
				var span = cells.attr("colspan");
				$('#EmptyDialog').data('span', span);
				
				if(cells.find("div").size() > 0)
				{
					cells.removeAttr("colspan");
					for(var i=span-1;i>0;i--)
					{
						var e = cells.clone();
						e.html("&nbsp;");
						e.attr("date",$.datepicker.parseDate("yy-mm-dd",e.attr("date")).addDate(i).toFormatString('yyyy-MM-dd'));
						cells.after(e);
					}
				}
			}
		},
		stop: function( event, ui ) {
			
			//ui.position
			var element = getElementFromPosition(ui.offset.left + 3, ui.offset.top + 3);
			if(element == null)
			{
				simpleDialog(messagebox_MoveFail);
			}
			else
			{
				var resId = $(this).attr("res");
				var res = $(this).attr("res");

				if($(this).attr("subId") != null)
					resId = $(this).attr("subId");
				var room = $(element).parents("[roomname]").attr("roomname");
	
				var buttons = [
					{ title:"OK" , callback: function(thisdialog,args){
						cancelClearRender(null);
						thisdialog.dialog("close");
						thisdialog.get(0).resId = res;
						moveReservationToSpecificRoom(res,args.resId,args.room,moveRoomSuccess,thisdialog);
					}},
					{ title:"Cancel" , callback: function(thisdialog,args){
						cancelClearRender(null);
						thisdialog.dialog("close");
					}}
				];
				customDialog(simpleLabel_Message,messagebox_MoveReservation,null,buttons,{resId: resId,room: room,res:res});
			}
		}});
}

function cancelClearRender(el)
{
	var el = $('#EmptyDialog').data('el');
	var res = $(el).attr("res");
	var span = $('#EmptyDialog').data('span');

	$("[res="+res+"]").not("[onclick]").remove();
	$("[res="+res+"][onclick]").css("display","");

	if(span != null && span > 1)
	{
		for(var i=1;i<span;i++)
		{
			var date = $.datepicker.parseDate("yy-mm-dd",$('#EmptyDialog').data('parent').attr("date"));
			$('#EmptyDialog').data('parent').parent().find("td[date='"+date.addDate(i).toFormatString('yyyy-MM-dd')+"']").remove();
		}
		if(span > 1)
			$('#EmptyDialog').data('parent').attr("colspan",span);
	}
	
	//reset span
	$('#EmptyDialog').data('span', 1);
	$('#EmptyDialog').data('parent', null);
	
}

function getElementFromPosition(positionX,positionY)
{
	var all = $("td[date]");
	for(var i=0;i<all.length;i++)
	{
		var minX = $(all[i]).offset().left;
		var maxX = minX + $(all[i]).width();
		var minY = $(all[i]).offset().top;
		var maxY = minY + $(all[i]).height();
		if(minX <= positionX && positionX <= maxX &&
			minY <= positionY && positionY <= maxY)
			return $(all[i]);
	}
	return null;
}

function moveReservationToSpecificRoom(res,resvId, roomId, callback,thisElement)
{
	var url = "/reservationService";
	var data = { func : "EditRoomSubReservation",
		resvId: resvId,
		roomId: roomId
	}
	if(isOpenResTab(res)) data.min = 1;
	waitingDialog({});
	simpleserviceWithArgs(url,data,callback,thisElement);
}
function moveReservationToSpecificRoomWithAssign(resvId, roomId, callback,thisElement)
{
	var url = "/reservationService";
	var data = { func : "EditRoomSubReservation",
		resvId: resvId,
		roomId: roomId,
		isAssign: true
	}
	waitingDialog({});
	simpleserviceWithArgs(url,data,callback,thisElement);
}
function moveRoomSuccess(data,thisdialog)
{
	var objArray = JSON.parse(data);
	if(objArray.EffectedRoom.length > 0)
	{
		for(var i=0;i<objArray.length;i++)
			changeRoom(objArray.EffectedRoom[i].id,objArray.EffectedRoom[i].roomId);

		if(objArray.ChargeList!=null){
			var Tabs = getTabsData(thisdialog.get(0).resId);
			ResetChargeList(objArray.ChargeList,Tabs);
		}
		cerltimespan(savedSpan,function(){setTimeout(function(){closeWaitingDialog();},200);});
	}
	else
	{
		simpleDialog(messagebox_MoveFail);
		cerltimespan(savedSpan);
	}
}
function isOpenResTab(res){
	var min = true;
	if(getObjectFromDataIndex(TabsData,"resId",res) != null)
		min = false;
	else if(TabsData.length > 0)
		for(var i=0;i<TabsData.length;i++)
			if(getObjectFromDataIndex(TabsData[i].GroupResvList,"resId",res) != null){
				min = false; break;
			}
	return min;
}